package com.example.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.model.dto.User;
import com.example.model.vo.ResponseResult;
import com.example.model.vo.UserVo;
import com.example.user.dao.UserMapper;
import com.example.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Author: ft
 * @Date:2025/8/25
 * @Description: 相关单据：
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseResult<? extends Object> login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user, userVo);
            String token = UUID.randomUUID().toString();
            userVo.setToken(token);
            stringRedisTemplate.opsForHash().put("token", token , user.getUsername());
            return ResponseResult.success(userVo);
        }
        return ResponseResult.error(HttpStatus.UNAUTHORIZED.value(),"用户名或密码错误");
    }
}